<?php
namespace app\weixin\controller;

class conn {
    
    public function index(){
        
            //微信公众平台开发接入实例代码
            
            define(TOKEN,'weixin1234');
            
            $timestamp = $_GET['timestamp'];
            $nonce     = $_GET['nonce'];
            $token     = TOKEN;
            
            $signature = $_GET['signature'];
            
            $tmpArr = array($timestamp,$nonce,$token);
            
            sort($tmpArr,SORT_STRING);
            
            $tmpStr = sha1(implode($tmpArr));
            
            if ($tmpStr == $signature  && $_GET['echostr']){
            
                echo $_GET['echostr'];
            
            }else{
            
                //echo '没有Echo Str ，FALSE';
                
                $this->response();
                
                
            }
        
    } //index func END
    
    
    
    public function response(){
        
        
        //echo "Response!";
        
 
        //1.获取到微信推送过来post数据（xml格式）
		$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
		//2.处理消息类型，并设置回复类型和内容
		/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</xml>*/
		$postObj = simplexml_load_string( $postArr );
		//$postObj->ToUserName = '';
		//$postObj->FromUserName = '';
		//$postObj->CreateTime = '';
		//$postObj->MsgType = '';
		//$postObj->Event = '';
		// gh_e79a177814ed
		//判断该数据包是否是订阅的事件推送
		if( strtolower( $postObj->MsgType) == 'event'){
			//如果是关注 subscribe 事件
			if( strtolower($postObj->Event == 'subscribe') ){
				//回复用户消息(纯文本格式)	
				$toUser   = $postObj->FromUserName;
				$fromUser = $postObj->ToUserName;
				$time     = time();
				$msgType  =  'text';
				$content  = '关注公众账号事件发生！';
				//$postObj->FromUserName.'-'.$postObj->ToUserName;
				$template = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							</xml>";
				$info     = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
				echo $info;
				
			} // event end
/*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你好]]></Content>
</xml>*/

		} // if end
		
		 if(strtolower($postObj->MsgType) == 'text'){
		     
		     
		     	$toUser   = $postObj->FromUserName;
				$fromUser = $postObj->ToUserName;
				$time     = time();
				$msgType  =  'text';
				
				
				
				if(trim($postObj->Content) == '123'){
				    
				    $content  = '哈哈哈 123！';
				    
				}
				else{
				$content  = '您输入了字符！';
				}
				
				
				
				
				
				//$postObj->FromUserName.'-'.$postObj->ToUserName;
				$template = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							</xml>";
				$info     = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
				echo $info;
		     
		     
		     
		 } //text END
		
		
		
		
        
    } //respose func END
    
            //获取用户基本信息
            public function getBase(){
                
                $appid = 'wxe37940b4d94c2fb5';
                
                $appsecret   = '9688ad925a8f8529b019ef6c0a7a46ae';
                
                $redirect = urlencode('http://xun.demo.yanxishe.cc/getcode/base');
                
                $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect."&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
                
                echo '<meta http-equiv="Refresh" content="0; url='.$url.'" /> ';
                
                //echo "Hello info";
                
            } //getBaseInfo func END
            
           
           //获取用户基本信息
            public function getInfo(){
                
                $appid = 'wxe37940b4d94c2fb5';
                
                $appsecret   = '9688ad925a8f8529b019ef6c0a7a46ae';
                
                $redirect = urlencode('http://xun.demo.yanxishe.cc/getcode/info');
                
                $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
                
                echo '<meta http-equiv="Refresh" content="0; url='.$url.'" /> ';
                
                //echo "Hello info";
                
            } //getBaseInfo func END
            
            
    

    
} // class END